//
//  QConfirmOrderInforMationVC.m
//  QoilPublic
//
//  Created by zhuang on 16/7/8.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QConfirmOrderInforMationVC.h"
#import "QConfirmOrderInforMationView.h"
#import "QSureOrderCell.h"
#import "QDetailsOrderVC.h"

#import "QAllOrderMoreVC.h"

#import "CaptchaTimerManager.h"
@interface QConfirmOrderInforMationVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UIImageView * backImage;
}
@property (nonatomic,strong)UITableView *tbView;
@end

@implementation QConfirmOrderInforMationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"确认订单信息";
    [self createUI];
}

- (void)createUI{
    
    WS(wSelf);

    backImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    backImage.image = [UIImage imageNamed:@"confirmOrderInformationBg"];
    [self.view addSubview:backImage];
    _tbView.layer.shadowColor = [QColorUtility defaultLowGrayColor].CGColor;
    _tbView.layer.shadowOpacity = 1;
    _tbView.layer.shadowOffset = CGSizeMake(0, 3);
    
    _tbView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
    _tbView.layer.cornerRadius = 1;
    
    CGFloat bottom = 0;
    if(iphone4x_3_5){
        bottom = -60;
    }else if (iphone5x_4_0){
        bottom = -100;
    }else if (iphone6_4_7){
        bottom = -130;
    }else if (iphone6Plus_5_5){
        bottom = -160;
    }else if (kDevice_Is_iPhoneX){
        bottom = -160;
    }
    
    [backImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wSelf.view.mas_top).offset((kNavHeight+kStatusHeight)+25);
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(bottom);
        make.left.equalTo(wSelf.view.mas_left).offset(20);
        make.right.equalTo(wSelf.view.mas_right).offset(-20);
    }];
    
    [self.view addSubview:self.tbView];
    
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wSelf.view.mas_top).offset((kNavHeight+kStatusHeight)+25);
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(bottom);
        make.left.equalTo(wSelf.view.mas_left).offset(40);
        make.right.equalTo(wSelf.view.mas_right).offset(-40);
    }];
    
    _tbView.scrollEnabled = NO;
    
    [_tbView registerNib:[UINib nibWithNibName:@"QSureOrderCell" bundle:nil] forCellReuseIdentifier:@"QSureOrderCell"];

    
   UIButton *_agreeButton = [QViewUtility createButtonWithFrame:CGRectZero font:17 title:@"确认订单" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    _agreeButton.tag = 4;
    _agreeButton.layer.cornerRadius = 5.0;
    _agreeButton.layer.shadowColor = [QColorUtility defaultBlueColor].CGColor;
    _agreeButton.layer.shadowOpacity = 1;
    _agreeButton.layer.shadowOffset = CGSizeMake(0, 2);
    [_agreeButton addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_agreeButton];
    
    CGFloat btnBottom = 0.0;
    if(iphone4x_3_5){
        btnBottom = -20;
    }else if (iphone5x_4_0){
        btnBottom = -40;
    }else if (iphone6_4_7){
        btnBottom = -50;
    }else if (iphone6Plus_5_5){
        btnBottom = -60;
    }else if (kDevice_Is_iPhoneX){
        btnBottom = -60;
    }
    
    [_agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(btnBottom);
        make.left.equalTo(wSelf.view.mas_left).offset(30);
        make.right.equalTo(wSelf.view.mas_right).offset(-30);
        make.height.equalTo(@45);
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (backImage.height - 20)/9;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 9;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     QSureOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QSureOrderCell" forIndexPath:indexPath];
    if (indexPath.row == 8) {
        if ([_model.oilType isEqualToString:@"国五"]) {
            cell.textLabel.text = @"\"温馨提示\":本次下单最低金额为500.00元";
        }else{
            cell.textLabel.text = @"\"温馨提示\":本次下单最低金额为1000.00元";
        }
    
        cell.textLabel.textColor = [UIColor redColor];
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        return cell;
    }
   
    cell.indexPath = indexPath;
    cell.model = self.model;
    return cell;
}

- (void)btnAction{
    
//    QPayMoneyVC *vc = [[QPayMoneyVC alloc] init];
//    vc.orderId = _model.orderId;
//    vc.moneyStr = _model.total;
//    [self.navigationController pushViewController:vc animated:YES];
    
    [self submitOrderRequestData:@"false"];
    
}

#pragma mark --- 提交订单接口
- (void)submitOrderRequestData:(NSString *)isFill{
    NSString *url = [QParamService urlWithAction:@"/order/saveOrder"];
    NSDictionary *param = [QParamService submitOrderName:_model.name time:_model.time city:_model.city limitArea:_model.limitArea address:_model.address lon:_model.lon lat:_model.lat tel:_model.tel flag:@"" remark:_model.remark OilTypeId:_model.oilTypeId OilPrice:_model.price InputPrice:_model.total FreightPrice:_model.costStr];
    
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            //避免重复下单  加入一个定时器
            CaptchaTimerManager *manager = [CaptchaTimerManager sharedTimerManager];
            manager.timeout = 30;
            [manager countDown];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"orderOrder" object:self];

            self.tabBarController.selectedIndex = 1;
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            if([basModel.errorCode isEqualToString:@"1000075"]){
                //[self requestTime];
            }
            if([basModel.errMsg isEqualToString:@"请重新登录"]){
                [QDataUtility deleteLoginModel];
            }
        }
        
    }];
}


- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor whiteColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}


@end
